<!--登录-->
<template>
  <div id="login">
    <div class="login-ui">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="邮箱/手机号" prop="name">
          <el-input v-model.trim="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model.trim="ruleForm.pass"></el-input>
        </el-form-item>

        <el-link class="forget" href="#">忘记密码</el-link>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

      <p>
        <!--   <el-link href="#/register">立即注册</el-link>-->
        <router-link to="/register" tag="el-link">立即注册</router-link>
      </p>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    // 校验密码是否为空
    let validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱/用户名'));
      } else {
        callback();
      }
    };

    // 校验密码是否为空
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };

    return {
      //表单
      ruleForm: {
        name: '',
        pass: '',
      },

      //验证规则
      rules: {
        name: [
          {validator: validateName, trigger: 'blur'}
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
      },

      //登录按钮默认禁用
      loginDisabled: true,
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let result = false;
          let user = null;
          let userArray = this.$cookies.keys(); // 获取所有 cookie name，以数组形式返回

          if (userArray.length !== 0) {
            // 检测用户输入的用户名，密码是否正确
            result = userArray.some((userId) => {
              user = this.$cookies.get(userId);

              if((user.phone === this.ruleForm.name || user.email === this.ruleForm.name) && user.pass === this.ruleForm.pass ) {
                return true;
              } else {
                return false;
              }
            })
          } else {
            alert('暂无用户');
          }

          // 用户名密码正确
          if (result) {
            this.$store.commit('changeIsLogin', true);
            // 更新用户的登录状态，并更新cookie
            user.isLogin = true;
            this.$cookies.set(`${user.id}`, user);

            this.$message({
              showClose: true,
              message: '登录成功',
              type: 'success',
              center: true
            });

            this.$cookies.set('defaultUserId', user.id); // 设置默认登录 id
            this.$router.push('/home'); // 跳转到首页
          } else {
            this.$message({
              showClose: true,
              message: '用户名或者密码错误',
              type: 'error',
              center: true
            });
          }

        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style lang="less" scoped>
@import "../public/less/login";
</style>